﻿using System;
using System.Data;
using System.Collections.Generic;

using Catering_Model;
namespace Catering_Rules
{
    /// <summary>
    /// OperationArea
    /// </summary>
    public partial class OperationArea
    {
        private readonly Catering_Operation.OperationArea dal = new Catering_Operation.OperationArea();
        public OperationArea()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return dal.Exists(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Catering_Model.OperationArea model)
        {
            dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Catering_Model.OperationArea model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            return dal.Delete(id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            return dal.DeleteList(idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.OperationArea GetModel(int id)
        {

            return dal.GetModel(id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public Catering_Model.OperationArea GetModelByCache(int id)
        {

            string CacheKey = "OperationAreaModel-" + id;
            object objModel = Catering_DBOpertion.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(id);
                    if (objModel != null)
                    {
                        int ModelCache = Catering_DBOpertion.ConfigHelper.GetConfigInt("ModelCache");
                        Catering_DBOpertion.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (Catering_Model.OperationArea)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere, bool IsAll = false)
        {
            return dal.GetList(strWhere, IsAll);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Catering_Model.OperationArea> GetModelList(string strWhere)
        {
            return GetModelList(strWhere, false);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Catering_Model.OperationArea> GetModelList(string strWhere, bool isziyingCompany)
        {
            DataSet ds = dal.GetList(strWhere, isziyingCompany);
            return DataTableToList(ds.Tables[0]);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Catering_Model.OperationArea> DataTableToList(DataTable dt)
        {
            List<Catering_Model.OperationArea> modelList = new List<Catering_Model.OperationArea>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Catering_Model.OperationArea model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Catering_Model.OperationArea();
                    if (dt.Rows[n]["id"] != null && dt.Rows[n]["id"].ToString() != "")
                    {
                        model.id = int.Parse(dt.Rows[n]["id"].ToString());
                    }
                    if (dt.Rows[n]["AreaName"] != null && dt.Rows[n]["AreaName"].ToString() != "")
                    {
                        model.AreaName = dt.Rows[n]["AreaName"].ToString();
                    }
                    if (dt.Rows[n]["AreaAdd"] != null && dt.Rows[n]["AreaAdd"].ToString() != "")
                    {
                        model.AreaAdd = dt.Rows[n]["AreaAdd"].ToString();
                    }
                    if (dt.Rows[n]["Back"] != null && dt.Rows[n]["Back"].ToString() != "")
                    {
                        model.Back = dt.Rows[n]["Back"].ToString();
                    }
                    if (dt.Rows[n]["temp1"] != null && dt.Rows[n]["temp1"].ToString() != "")
                    {
                        model.temp1 = dt.Rows[n]["temp1"].ToString();
                    }
                    if (dt.Rows[n]["temp2"] != null && dt.Rows[n]["temp2"].ToString() != "")
                    {
                        model.temp2 = dt.Rows[n]["temp2"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllListByCompanyID(int companyid)
        {
            if (companyid == 0) return GetList(""); else return GetList(" temp2=" + companyid + " ");
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllListByCompanyID(int companyid, bool flag)
        {
            if (companyid == 0) return GetList("", flag); else return GetList(" temp2=" + companyid + " ", flag);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Catering_Model.OperationArea> GetAllList(int companyid)
        {
            return GetAllList(companyid, false);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Catering_Model.OperationArea> GetAllList(int companyid, bool isziyingcompany)
        {
            List<Catering_Model.OperationArea> listOArea = new List<Catering_Model.OperationArea>();
            DataTable dt = new DataTable();
            if (companyid == 0)
            {
                dt = GetList("", isziyingcompany).Tables[0];
            }
            else
            {
                dt = GetList(" temp2=" + companyid + " ", isziyingcompany).Tables[0];
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Catering_Model.OperationArea temp = new Catering_Model.OperationArea();
                temp.id = Int32.Parse(dt.Rows[i]["id"].ToString());
                temp.AreaName = dt.Rows[i]["AreaName"].ToString();
                listOArea.Add(temp);
            }
            return listOArea;
        }

        /// <summary>
        /// 获取用户所在公司下的所有班组
        /// </summary>
        /// <param name="PID"></param>
        /// <returns></returns>
        public DataTable GetAreaIDListByPersonnelID(int PID)
        {
            Catering_Model.E_Personnel PSM = new Catering_Operation.D_Personnel().GetModel(PID);
            if (PSM != null && PSM.ClassID > 0)
            {
                return dal.GetCommpanyAreaIDListByClassID(PSM.ClassID);
            }
            return null;
        }

        /// <summary>
        /// 获取用户所在公司下的所有班组
        /// </summary>
        /// <param name="PID"></param>
        /// <returns></returns>
        public string GetStrAreaIDListByPersonnelID(string StrPID)
        {
            string result = "";
            int PID = 0;
            Int32.TryParse(StrPID, out PID);
            if (PID > 0)
            {
                DataTable dt = GetAreaIDListByPersonnelID(PID);
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        int classid = Convert.ToInt32(dt.Rows[i]["AreaID"]);
                        if (classid > 0) result += classid + ",";
                    }
                }
            }
            return result.ToString().Length > 0 ? result.ToString().TrimEnd(',') : "";
        }

        public string GetStrAreaIDListByCompanyID(int companyid)
        {
            string result = "";
            DataTable dt = dal.GetCommpanyAreaIDListByCompanyID(companyid);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int areaid = Convert.ToInt32(dt.Rows[i]["AreaID"]);
                    if (areaid > 0) result += areaid + ",";
                }
                dt.Dispose();
            }
            return result.ToString().Length > 0 ? result.ToString().TrimEnd(',') : "";
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  Method
    }
}

